<?php
/**
 * 添加js文件
 * @param unknown $dirname
 * @param unknown $file_name
 */
function js($dirname, $file_name, $version=1.0)
{
    echo '<script type="text/javascript" src="'.$dirname.'/js/'.$file_name.'.js?v='.$version.'"></script>';
}

/**
 * 添加css文件
 * @param unknown $dirname
 * @param unknown $file_name
 */
function css($dirname, $file_name, $version=1.0)
{
    echo '<link rel="stylesheet" type="text/css" href="'.$dirname.'/css/'.$file_name.'.css?v='.$version.'"/>';
}

/**
 * 返回html提示信息
 * @return string
 */
function execute_alert_message()
{
    $CI = & get_instance();
    $CI->load->library('session');
    if ($CI->session->flashdata('success')) {
        return '<div class="am-alert" data-am-alert><button type="button" class="am-close">&times;</button><p>'. $CI->session->flashdata('success') . '</p></div>';
    } elseif ($CI->session->flashdata('error')) {
        return '<div class="am-alert" data-am-alert><button type="button" class="am-close">&times;</button><p>'. $CI->session->flashdata('error') . '</p></div>';
    }
}

function warning_alert_message($warning)
{
    return '<div class="alert">
                <button class="close" data-dismiss="alert"></button>
                <strong>Warning!</strong> ' . $warning . '
            </div>';
}

function info_alert_message($info)
{
    return '<div class="alert-info">
                <button class="close" data-dismiss="alert"></button>
                <strong>Warning!</strong> ' . $info . '
            </div>';
}

/**
 * 例如: 数组最后一个不用填写key值，可接受数组和字符串。
 * array(
 *       'admin/linemanage/grid'=>'线路管理',
 *       '添加线路'
 *      )
 */
function breadcrumb($urlInfo)
{
    $breadcrumb = '<ul class="breadcrumb">';
    if (is_array($urlInfo)) {
        foreach ($urlInfo as $url => $name) {
            if (!is_numeric($url)) {
                $breadcrumb .= '<li>
                                    <a href="' . site_url($url) . '">'.$name.'</a>
                                    <i class="icon-angle-right"></i>
                                </li>';
            } else {
                $breadcrumb .= '<li>'.$name.'<i class="icon-angle-right"></i></li>';
            }
        }
    } else {
        $breadcrumb .= '<li>'.$urlInfo.'<i class="icon-angle-right"></i></li>';
    }

    $breadcrumb .= '</ul>';
    return $breadcrumb;
}

/**
 * 获取时间的周
 * @return string
 */
function get_week_name($date)
{
    $weekarray=array('日','一','二','三','四','五','六'); //先定义一个数组
    return '周'.$weekarray[date('w',strtotime($date))];
}

/**
 *  对象转换成数组
 * @param unknown $obj
 * @return unknown
 */
function ob2ar($obj)
{
    if (is_object($obj)) {
        $obj = (array)$obj;
        $obj = ob2ar($obj);
    } else if (is_array($obj)) {
        foreach ($obj as $key => $value) {
            $obj[$key] = ob2ar($value);
        }
    }
    return $obj;
}

/**
 * 可视化打印。
 * @param mixed $data
 */
function pr($data)
{
    echo '<pre>';
    print_r($data);
    echo '</pre>';
}

/**
 * 可视化打印。
 * @param mixed $data
 */
function dump($data)
{
    echo '<pre>';
    var_dump($data);
    echo '</pre>';
}

function redirectAction($uri = '', $method = 'location', $http_response_code = 302)
{
    if (!preg_match('#^https?://#i', $uri)) {
        $uri = site_url($uri);
    }

    switch($method) {
        case 'refresh' : 
            header("Refresh:0;url=".$uri);
            break;
        default : 
            header("Location: ".$uri, TRUE, $http_response_code);
            break;
    }
    exit;
}

//验证是不是微信5.0 版本
function validateWeiXin()
{
    $weiXinStatus  =  false;
    if (strpos($_SERVER['HTTP_USER_AGENT'], "MicroMessenger") !== false ) {
        $weiXinStatus = true;
    }
    return $weiXinStatus;
}

//验证是否是支付宝
function validateAliapp()
{
    return strpos($_SERVER['HTTP_USER_AGENT'], "Alipay");
}

/**
 * 生成订单sn
 * @return string
 */
function getOrderSn()
{
    return date('ynjGis') . mt_rand(100, 999);
}

/**
 * 商品快递物流
 * @return array
 */
function tourismLogisticsTypeArray()
{
    return array(
        'ems'            => 'EMS',
        'shunfeng'       => '顺丰速递',
        'shentong'       => '申通速递',
        'yuantong'       => '圆通速递',
        'zhongtong'      => '中通快递',
        'huitongkuaidi'  => '百世汇通',
        'yunda'          => '韵达快递',
        'zhaijisong'     => '宅急送',
        'tiantian'       => '天天快递',
        'guotongkuaidi'  => '国通快递',
        'zengyisudi'     => '增益速递',
        'ztky'           => '中铁物流',
        'zhongtiewuliu'  => '中铁快运',
        'ganzhongnengda' => '能达速递',
        'quanfengkuaidi' => '全峰快递',
        'jd'             => '京东',
        'bjemstckj'      => '北京EMS',
        'huiqiangkuaidi' => '汇强快递',
        'yuntongkuaidi'  => '运通快递',
        'kuaijiesudi'    => '快捷速递',
        'youzhengguonei' => '邮政包裹',
        'debangwuliu'    => '德邦',
        'tiandihuayu'    => '天地华宇',
        'suer'           => '速尔物流',
        'youshuwuliu'    => '优速',
        'yuanchengwuliu' => '远成物流',
        'jiayiwuliu'     => '佳怡物流',
        'longbanwuliu'   => '龙邦物流',
        'hengluwuliu'    => '恒路物流',
        'xinbangwuliu'   => '新邦物流',
        'tnt'            => 'TNT',
        'dhl'            => 'DHL',
        'fedex'          => 'Fedex',
        'ups'            => 'UPS',
        'usps'           => 'USPS',
    );
}

/**
 * 删除所有空格,前，后，中间空格都去掉
 * @param unknown $str
 * @return mixed
 */
function trimall($str)
{
    $qian=array(" ","　","\t","\n","\r","&nbsp;");$hou=array("","","","","","");
    return str_replace($qian,$hou,$str);
}

/**
 * 删除指定key对应value值
 * @param unknown $data
 * @param unknown $key
 * @return unknown
 */
function array_remove($data, $key){
    if(!array_key_exists($key, $data)){
        return $data;
    }
    $keys = array_keys($data);
    $index = array_search($key, $keys);
    if($index !== FALSE){
        array_splice($data, $index, 1);
    }
    return $data;
}

/**
 *  剩余时间
 */
function time_tran($the_time)
{
    $now_time = date("Y-m-d H:i:s", time());
    $now_time = strtotime($now_time);
    $show_time = strtotime($the_time);
    $dur = $now_time - $show_time;
    if ( $dur < 0 ) {
        return $the_time;
    } else {
        if ( $dur < 60 ) {
            return $dur . '秒前';
        } else {
            if ( $dur < 3600 ) {
                return floor($dur / 60) . '分钟前';
            } else {
                if ( $dur < 86400 ) {
                    return floor($dur / 3600) . '小时前';
                } else {
                    if ( $dur < 259200 ) {//3天内
                        return floor($dur / 86400) . '天前';
                    } else {
                        return $the_time;
                    }
                }
            }
        }
    }
}

/**
 * 搜索记录记录
 * @param unknown $tourismGoods
 * @param $type=1 景区  $type=2 商城  $type=3 酒店  $type=4线路
 */
function seeHistory($keyword, $type=1)
{
    $historyPram = array();
    $historyCookie = get_cookie('historySearch');
    if ( !empty($historyCookie) ) {
        $historyPram = unserialize($historyCookie);
        foreach ( $historyPram as $key => $val ) {
            $arr2[] = $val['keyword'];
        }
        if ( !in_array($keyword, $arr2) ) {
            $data['type'] = $type;
            $data['keyword'] = $keyword;
            array_unshift($historyPram, $data);
        }
        if ( count($historyPram) > 16 ) {
            array_pop($historyPram);
        }
    } else {
        $historyPram[0]['type'] = $type;
        $historyPram[0]['keyword'] = $keyword;
    }
    set_cookie('historySearch', serialize($historyPram), 435200);
}

//curl
function fn_get_contents( $url, $keysArr = array(), $mothod = 'get', $is_header = 1, $flag = 0 )
{
    $ch = curl_init() ;
    if (!$flag) {
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false) ;
    }
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE) ;
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE) ;

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ;
    if (strtolower($mothod) == 'post') {
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $keysArr) ;
    } else {
        $url = $url . "?" . http_build_query($keysArr) ;
    }
    curl_setopt($ch, CURLOPT_URL, $url) ;

    $ret = curl_exec($ch) ;
    curl_close($ch) ;
    return $ret ;
}
